<?php
namespace Home\Controller;
use Think\Controller;

class RequirementController extends BaseController
{
    /**
     * 个人中心--我的信息
     * $data 登录的用户信息
     */
    public function personalCenter()
    {
        $email = $_SESSION['Home']['email'];
        $data = D("User")->selectLoginInfo("u_email='$email'");
        
        $this->assign("pc_data", $data);
        $this->display();
    }

    /**
     * 个人中心--我的项目
     * $data 登录的用户信息
     */
    public function pcMyProjects(){
        $yhid = $_SESSION['Home']['yhid'];
        $pronumData = D("Requirement")->selectMyProjectNum("r_submitId='$yhid'");
        $projectData = $this->showMyProjectsPages("r_submitId='$yhid'");

        if ($pronumData == null) {
            $this->assign("pnum_data", 0);
        } else {
            $this->assign("pnum_data", $pronumData);
        }
        $this->assign("pro_data", $projectData);
        $this->display();
    }
    
    /**
     * 提交需求--左
     * $data 提交信息
     * $nextId 自增的下一个id
     * $nowDate 当前时间
     */
    public function submitReqLeftClick()
    {
        $data = array();
        $data['r_contactName'] = trans_p("n");
        $data['r_contactPhone'] = trans_p("p");
        $data['r_industry'] = trans_p("i");
        $data['r_consultBudget'] = trans_p("b");
        
        $nextId = D("Requirement")->getNextAutoIncrementId();
        $nowDate = date("ymd", time());
        $data['r_xmId'] = "XM" . $nowDate . num_format($nextId);
        $data['r_projectTitle'] = 5; // 创业咨询
        
        $data['r_submitTime'] = date("Y-m-d H:i:s", time());
        $data['r_submitId'] = $_SESSION['Home']['yhid'];
        
        $left_data = D("Requirement")->insertIntoReqForSubmitLeft($data);
        if ($left_data) {
            $this->ajaxReturn(array(
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    public function getProjectTitleAndService()
    {
        $_SESSION['Home']['ptitle'] = trans_p("pt");
        $_SESSION['Home']['pService'] = trans_p("ps");
        dump($_SESSION['Home']['ptitle']);
    }

    /**
     * 提交需求--右-第二步
     * $reqtwo_data 提交需求信息
     */
    public function submitReqRightTwoClick()
    {
        $reqtwoData = array();
        
        $reqtwoData['r_contactName'] = trans_p("submitReqRight_name");
        $reqtwoData['r_contactPhone'] = trans_p("submitReqRight_phone");
        $reqtwoData['r_projectName'] = trans_p("submitReqRight_pname");
        $reqtwoData['r_projectBudget'] = trans_p("submitReqRight_pbudget");
        $reqtwoData['r_projectResume'] = trans_p("submitReqRight_presume");
        $reqtwoData['r_projectCycle'] = trans_p("zq");
        
        $reqtwoData['r_projectTitle'] = $_SESSION['Home']['ptitle'];
        $reqtwoData['r_projectService'] = $_SESSION['Home']['pService'];
        
        $nextId = D("Requirement")->getNextAutoIncrementId();
        $nowDate = date("ymd", time());
        $reqtwoData['r_xmId'] = "XM" . $nowDate . num_format($nextId);
        // $data['r_projectTitle']=5;//创业咨询
        
        $reqtwoData['r_submitTime'] = date("Y-m-d H:i:s", time());
        $reqtwoData['r_submitId'] = $_SESSION['Home']['yhid'];
        
        if ($_FILES['file']['name'][0] == "") { // 没有文件上传
            $rightData = D("Requirement")->insertIntoReqForSubmitLeft($reqtwoData);
            if ($rightData) {
                $this->ajaxReturn(array(
                    'status' => 1
                ));
            } else {
                $this->ajaxReturn(array(
                    'status' => 0
                ));
            }
            return false;
        }
        $_SESSION['Home']['xmid'] = $reqtwoData['r_xmId']; // 项目id
        
        $str = '';
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 5 * 1024 * 1024; // 设置附件上传大小
        // $upload->exts = array('txt','doc','docx'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->subName = date("ymdHis", time()) . $_SESSION['Home']['email']; // 上传子目录
        for ($i = 0; $i < count($_FILES['file']['name']); $i ++) {
            $phoneimg = array();
            $phoneimg['name'] = $_FILES['file']['name'][$i];
            $phoneimg['tmp_name'] = $_FILES['file']['tmp_name'][$i];
            $phoneimg['type'] = $_FILES['file']['type'][$i];
            $phoneimg['error'] = $_FILES['file']['error'][$i];
            $phoneimg['size'] = $_FILES['file']['size'][$i];
            $upload->saveName = strstr($_FILES['file']['name'][$i], ".", true);
            $str .= $upload->subName . "/" . $_FILES['file']['name'][$i] . ";";
            $info = $upload->uploadOne($phoneimg);
        }
        
        if (! $info) { // 上传错误提示错误信息
            $error = $upload->getError();
            $this->ajaxReturn(array(
                'info' => $error,
                'status' => 2
            ));
            return false;
        }
        
        $rightData = D("Requirement")->insertIntoReqForSubmitLeft($reqtwoData);
        if (! $rightData) {
            $this->ajaxReturn(array(
                'status' => 0
            ));
            return false;
        }
        
        $xmid = $_SESSION['Home']['xmid'];
        $modData = array();
        $modData['r_uploadFile'] = $str;
        $uploadData = D("Requirement")->getXmIdToModUpload($modData, "r_xmId='$xmid'");
        if ($uploadData) {
            $this->ajaxReturn(array(
                'status' => 1
            ));
            return false;
        } else {
            $this->ajaxReturn(array(
                'status' => 3
            ));
            return false;
        }
    }
    
    /**
     * 邮箱号唯一
     * $mailNum 邮箱号
     */
    public function emailUnique()
    {
        $mailNum = trans_p('mailNumber');
        $data = D("User")->judgeEmailUnique("u_email='$mailNum'", "u_email");
        if ($data) {
            $this->ajaxReturn(array( // 已存在
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array( // 不存在
                'status' => 0
            ));
        }
    }

    /**
     * 邮箱号唯一
     * $mailNum 邮箱号
     */
    public function emailUniqueExceptSelf()
    {
        $mailNum = trans_p('mailNumber');
        $mailSelf = $_SESSION['Home']['email'];
        $data = D("User")->judgeEmailUnique("u_email='$mailNum' and u_email != '$mailSelf'", "u_email");
        if ($data) {
            $this->ajaxReturn(array( // 已存在
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array( // 不存在
                'status' => 0
            ));
        }
    }

    /**
     * 手机号唯一
     * $phoneNum 手机号
     * $data 查找结果集
     */
    public function phoneUnique()
    {
        $phoneNum = trans_p('phoneNum');
        $data = D("User")->judgePhoneUnique("u_phone='$phoneNum'", "u_phone");
        if ($data) {
            $this->ajaxReturn(array(
                'status' => 1
            ));
        } else {
            $this->ajaxReturn(array(
                'status' => 0
            ));
        }
    }

    /**
     * 个人中心--个人信息--保存--修改表信息
     */
    public function pcSave()
    {
        $mod_data = array();
        $mod_data['u_email'] = trans_p("e");
        $mod_data['u_phone'] = trans_p("p");
        $mod_data['u_name'] = trans_p("n");
        $mod_data['u_weixin'] = trans_p("wx");
        $mod_data['u_qq'] = trans_p("q");
        $email = $_SESSION['Home']['email'];
        $data = D("User")->modUserForPC($mod_data, "u_email='$email'");
        if ($data === false) {
            $this->ajaxReturn(array(
                'status' => 0
            ));
            return false;
        }
        $_SESSION['Home']['email'] = $mod_data['u_email'];
        $_SESSION['Home']['sub_email'] = substr_replace($mod_data['u_email'], "****", 3,4);
        $this->ajaxReturn(array(
            'status' => 1
        ));
        return false;
    }

    /**
     * 退出到serviceCenter
     */
    public function exitToSC()
    {
        unset($_SESSION['Home']);
        $_SESSION['Home']['uid'] = 2;
        $this->redirect("User/serviceCenter");
    }

    /**
     * 退出到首页
     */
    public function exitToUser()
    {
        unset($_SESSION['Home']);
        $this->redirect("User/main");
    }
}
